//
//  WaterFallLayout.m
//  Ui-Lesson-21-03
//
//  Created by lanou3g on 15/11/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "WaterFallLayout.h"
@interface WaterFallLayout()
//item 的数量
@property(nonatomic,assign)NSInteger numberOfItems;
//保存每列的高度
@property(nonatomic,strong)NSMutableArray *columsHeights;
//存放item属性的数组
@property(nonatomic,strong)NSMutableArray *itemAttributes;
//获取最长列的索引
-(NSInteger) p_indexForlongestColumn;
//获取最短列的索引
-(NSInteger) p_indexForShortestColumn;

@end
@implementation WaterFallLayout
//获取最长列的索引

//懒加载
-(NSMutableArray *)columsHeights{
    if (!_columsHeights) {
        self.columsHeights = [NSMutableArray array];
    }
    return _columsHeights;

}
//懒加载
-(NSMutableArray *)itemAttributes{
    if (!_itemAttributes) {
        self.itemAttributes = [NSMutableArray array];
    }
    return _itemAttributes;
    
}
//获取最长列的索引
-(NSInteger) p_indexForlongestColumn{
    //记录哪个是最长的
    NSInteger longgestHight = 0;
    //记录当前最长的item的下标
    NSInteger longesstItemindex = 0;
    //循环,选出最长的item的索引
    for (int i = 0; i<self.numebrOfColumns; i++) {
        CGFloat currentHeight = [self.columsHeights[i] floatValue];
        if (longgestHight <currentHeight) {
            longgestHight = currentHeight;
            longesstItemindex = i;
        }
    }
    //返回
    return  longesstItemindex;

}
//获取最短列的索引
-(NSInteger) p_indexForShortestColumn{
//记录最短列的索引
    NSInteger shortestItemIndex = 0;
    //记录最短高度
    CGFloat shortestHeight = MAXFLOAT;//浮点型最大值
    //寻找最短列的索引
    for (int i = 0; i<self.numebrOfColumns; i++) {
            CGFloat currentHeight = [self.columsHeights[i] floatValue];
   //判断找到最短的高
        if (shortestHeight>currentHeight) {
            shortestHeight = currentHeight;
            shortestItemIndex = i;
        }
    }
    return shortestItemIndex;
}
-(void)prepareLayout{
    //执行父类的方法
    [super prepareLayout];
    //给高度数组赋值(top的高度)
    for (int i = 0; i<self.numebrOfColumns; i++) {
        self.columsHeights[i] =@( self.sectionInsets.top );//@(x)转化数值类型
        
    }
//获取到item的个数
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0];
//为每一个item设置frame
    for (int i = 0; i<self.numberOfItems; i++) {
        //获取到高度最小的列
   NSInteger shortestIndex  = [self p_indexForShortestColumn];
        //获取到最小的高度
        CGFloat shortestH = [self.columsHeights[shortestIndex] floatValue];
        //设置x值 内边距 left +(item +item间距) *列的索引
        CGFloat detalX = self.sectionInsets.left +(self.itemSize.width +self.insertItemSpacing) *shortestIndex;
        //设置y值 最短的高 +间距
        CGFloat detalY = shortestH +self.insertItemSpacing;
        //设置indexPath
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        //获取item的高度
        CGFloat itemHeight = 0;
        if ([self.delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
        itemHeight   =  [self.delegate heightForItemIndexPath:indexPath];
        }
        
        
        attributes.frame = CGRectMake(detalX, detalY, self.itemSize.width, itemHeight);
        //添加到数组中用做layoutAttributForelementsRect方法的返回值,从而根据数组设置来设置item
        [self.itemAttributes addObject:  attributes];
        //更新高度数组
        self.columsHeights[shortestIndex] =@( detalY +itemHeight);
        
    }
}
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.itemAttributes;
}
-(CGSize)collectionViewContentSize{
//最长高度 +距下距离
    //获取最长高度的索引
    NSInteger longestindex = [self p_indexForShortestColumn];
    //通过索引获取最长列的长度
    CGFloat longestH = [self.columsHeights[longestindex] floatValue];
    //最长的高度 +距下距离
    CGFloat height =  longestH +self.sectionInsets.bottom;
    //修改contentSize

    CGSize contentSize =  self.collectionView.frame.size;
    contentSize.height =height;
    
    //将修改之后的contentSize返回
    return contentSize;


}









@end
